﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using CellularAutomation.Core;
using CellularAutomation.IView;

namespace CellularAutomation.GUI
{
	public partial class FormView : Form, ICellAutoView
	{
		public FormView()
		{
			InitializeComponent();
		}


		#region Implementation of ICellAutoView

		public void SetAliveCellCoordList(IList<Point> cellList)
		{
			
			var list = new List<Point>(10) {
			                               	new Point(5, 5)
			                               };
			this.gridField.Data = list;

			//this.gridField.Data = cellList;
		}

		public event EventHandler<CellStateChangedEventArgs> CellStateChanged;
		private void OnCellStateChanged(CellStateChangedEventArgs e)
		{
			EventHandler<CellStateChangedEventArgs> handler = this.CellStateChanged;
			if (handler != null) handler(this, e);
		}

		public event EventHandler<EventArgs> NextOneStep;
		private void OnNextOneStep(EventArgs e)
		{
			EventHandler<EventArgs> handler = this.NextOneStep;
			if (handler != null) handler(this, e);
		}

		public event EventHandler<CreateGridEventArgs> CreateGrid;
		private void OnCreateGrid(CreateGridEventArgs e)
		{
			EventHandler<CreateGridEventArgs> handler = this.CreateGrid;
			if (handler != null) handler(this, e);
		}

		#endregion


		private void ButtonNextStepClick(object sender, EventArgs e)
		{
			this.OnNextOneStep(new EventArgs());
		}

		private void ButtonCreateGridClick(object sender, EventArgs e)
		{
			try
			{
				var sizeX = Int32.Parse(this.textBoxX.Text);
				var sizeY = Int32.Parse(this.textBoxY.Text);
				if (sizeX <= 0 | sizeY <= 0)
					throw new ArgumentOutOfRangeException("sender","Размер решетки не может быть нулем или меньше нуля");
				this.OnCreateGrid(new CreateGridEventArgs(sizeX, sizeY));
			}
			catch (Exception exception)
			{
				MessageBox.Show(exception.Message, exception.Source);
			}

		}
	}
}
